package com.itmuch.cloud.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.itmuch.cloud.entity.User;
import com.itmuch.cloud.repository.UserRepository;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;

@RestController
public class UserController {
	
	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private EurekaClient eurekaClient;
	
	@Autowired
	private DiscoveryClient discoveryClient;
	
	@GetMapping("/simple/{id}")
	public User findById(@PathVariable("id") Long id) {
		return userRepository.findOne(id);
	}
		
	
	//获取到ip号,端口号，很多参数
	@GetMapping("/instance")
	public String serviceUrl() {
		InstanceInfo info = this.eurekaClient.getNextServerFromEureka("MICROSERVICE-PROVIDER-USER", false);
		return info.getHomePageUrl();
	}
	
	//可以查看到这台机器的端口，ip,服务
	@GetMapping("/disClient")
	public ServiceInstance disClient(){
		ServiceInstance localServiceInstance = discoveryClient.getLocalServiceInstance();
		return localServiceInstance;
	}
		
	@PostMapping("/user")
	public User findPost(@RequestBody User user) {
		System.out.println(user);
		return user;
	}
	
	@GetMapping("/findget")
	public User findGet(@RequestBody User user) {
		System.out.println(user);
		return user;
	}
	
	@GetMapping("/findlist")
	public List<User> findList() {
		User user1 = new User(1l,"小明");
		User user2 = new User(2l,"小红");
		User user3 = new User(3l,"小李");
	    ArrayList<User> list = Lists.newArrayList();
	    list.add(user1);
	    list.add(user2);
	    list.add(user3);
		return list;
	}
	
	
}
